
#include "cx_simple_modulemgr.h"

QINT modulecompare (QVOID * pFirst, QVOID * pSecond)
{
	IModule* pModuleFirst = pFirst;
	IModule* pModuleSecond = *(IModule**)pSecond;
	if(pModuleFirst&&pModuleSecond)
	{
		if(pModuleFirst->moduleid_== pModuleSecond->moduleid_)
			return 0;
		if(pModuleFirst->moduleid_> pModuleSecond->moduleid_)
			return 1;
		else
			return -1;
	}
	return 1;
}

int CXSimpleModuleMgr_registermodule (IModuleManager* t,IModule* pModule)
{
	CXSimpleModuleMgr* pMgr = SUB_PTR(t,IModuleManager,CXSimpleModuleMgr);
	void* pResult = NULL;
	QINT nSize = 0;
	cx_vector_find(pMgr->vctmodules_,pModule,&pResult,&nSize,modulecompare);
	if(!pResult)
	{
		cx_vector_append(pMgr->vctmodules_,&pModule,sizeof(pModule));
		return 0;
	}
	return -1;//module exist
}

int CXSimpleModuleMgr_unregistermodule (IModuleManager* t,int moduleid)
{
	CXSimpleModuleMgr* pMgr = SUB_PTR(t,IModuleManager,CXSimpleModuleMgr);
	void* pResult = NULL;
	QINT nSize = 0;
	QINT index = -1;
	IModule* pTmp = (IModule*)cx_malloc(sizeof(IModule));
	pTmp->moduleid_ = moduleid;
	index = cx_vector_find(pMgr->vctmodules_,pTmp,&pResult,&nSize,modulecompare);
	cx_free(pTmp);
	if(index>=0)
	{
		cx_vector_removeElementAt(pMgr->vctmodules_,index,&nSize);
		return 0;
	}
	return -1;
}

IModule* CXSimpleModuleMgr_getmodule (IModuleManager* t,int moduleid)
{
	CXSimpleModuleMgr* pMgr = SUB_PTR(t,IModuleManager,CXSimpleModuleMgr);
	void* pResult = NULL;
	IModule* pModule = NULL;
	QINT nSize = 0;
	QINT index = -1;
	IModule* pTmp = (IModule*)cx_malloc(sizeof(IModule));
	pTmp->moduleid_ = moduleid;
	cx_vector_find(pMgr->vctmodules_,pTmp,&pResult,&nSize,modulecompare);
	cx_free(pTmp);
	if(pResult)
	{
		pModule = *(IModule**)pResult;
		return pModule;
	}
	return NULL;
}

int CXSimpleModuleMgr_sendmessage (IModuleManager* t,int message, unsigned int wParam, unsigned int lParam,int toModlueid,int fromModuleid)
{
	CXSimpleModuleMgr* pMgr = SUB_PTR(t,IModuleManager,CXSimpleModuleMgr);
	void* pResult = NULL;
	IModule* pModule = NULL;
	QINT nSize = 0;
	IModule* pTmp = (IModule*)cx_malloc(sizeof(IModule));
	pTmp->moduleid_ = toModlueid;
	cx_vector_find(pMgr->vctmodules_,pTmp,&pResult,&nSize,modulecompare);
	cx_free(pTmp);
	if(pResult)
	{
		pModule = *(IModule**)pResult;
		return pModule->handlemessage(pModule,message,wParam,lParam,fromModuleid);
	}
	return -1;
}
int CXSimpleModuleMgr_postmessage (IModuleManager* t,int message, unsigned int wParam, unsigned int lParam,int toModlueid,int fromModuleid)
{
	return -1;
}

void CXSimpleModuleMgr_retain(IModuleManager* t)
{
	t->refcount_++;
}

void CXSimpleModuleMgr_releasewithsuperptr(IModuleManager* t)
{
	CXSimpleModuleMgr* pMgr = SUB_PTR(t,IModuleManager,CXSimpleModuleMgr);
	CXSimpleModuleMgr_release(pMgr);
}

CTOR(CXSimpleModuleMgr)
	FUNCTION_SETTING(IModuleManager.registermodule, CXSimpleModuleMgr_registermodule);
	FUNCTION_SETTING(IModuleManager.unregistermodule, CXSimpleModuleMgr_unregistermodule);
	FUNCTION_SETTING(IModuleManager.getmodule, CXSimpleModuleMgr_getmodule);
	FUNCTION_SETTING(IModuleManager.sendmessage, CXSimpleModuleMgr_sendmessage);
	FUNCTION_SETTING(IModuleManager.postmessage, CXSimpleModuleMgr_postmessage);
	FUNCTION_SETTING(IModuleManager.retain, CXSimpleModuleMgr_retain);
	FUNCTION_SETTING(IModuleManager.release, CXSimpleModuleMgr_releasewithsuperptr);
	cthis->IModuleManager.refcount_ = 1;
	cthis->vctmodules_ = cx_vector_new(sizeof(void*),20);
END_CTOR

DTOR(CXSimpleModuleMgr)
	cthis->IModuleManager.refcount_--;
	if(cthis->IModuleManager.refcount_>0)
	{
		return 0; // return 0; not free the object
	}
	else
	{
		//add free code here
		//call super class type_dtor to free super class member	
		cx_vector_releaseRef(cthis->vctmodules_);
	}
END_DTOR
